﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace GeometryViewerUI
{
    /// <summary>
    /// Interaction logic for GraphLIstEditDialog.xaml
    /// </summary>
    public partial class GraphLIstEditDialog : Window
    {
        private SqlConnection conn;
        public ObservableCollection<DatabaseEntityWrapper> databaseList { get; set; }
        public GraphLIstEditDialog(SqlConnection conn)
        {
            InitializeComponent();
            databaseList = new ObservableCollection<DatabaseEntityWrapper>();
            /*databaseList.Add(new DatabaseEntityWrapper { id = 1, FileName = "first" });
            databaseList.Add(new DatabaseEntityWrapper { id = 2, FileName = "second", KeyWords="asdf", Include = true });
            databaseList.Add(new DatabaseEntityWrapper { id = 3, FileName = "third", KeyWords = "asdf", Include = true });
            databaseList.Add(new DatabaseEntityWrapper { id = 4, FileName = "forth", KeyWords = "asdf", Include = true });*/
            this.DataContext = this;

            this.conn = conn;

        }

        private void SaveListAndClose_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }

        private void GetFilteredRecords_Click(object sender, RoutedEventArgs e)
        {
            conn.Open();
            //GraphListDataGrid.bin
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT id, fileName, ts, '' as keywords from Graphs";
            using (SqlDataReader rdr = cmd.ExecuteReader())
            {
                rdr.Read();
                do
                {
                    DatabaseEntityWrapper wr = new DatabaseEntityWrapper();
                    wr.id = Convert.ToInt32(rdr[0]);
                    wr.FileName = Convert.ToString(rdr[1]);
                    wr.Date = Convert.ToString(rdr[2]);
                    wr.KeyWords = Convert.ToString(rdr[3]);
                    databaseList.Add(wr);
                } while (rdr.Read());
            }
            conn.Close();
        }
        private void UpdateDatabase_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Not implemented");
        }
    }


    public class DatabaseEntityWrapper : INotifyPropertyChanged
    {
        private int idValue = 0;
        private string FileNameValue = "";
        private string DateValue = DateTime.Now.ToString();
        private string KeyWordsValue = "";
        private bool IncludeValue = true;

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        public int id
        {
            get
            {
                return this.idValue;
            }
            set
            {
                if (value != this.idValue)
                {
                    this.idValue = value;
                    NotifyPropertyChanged("id");
                }
            }
        }
        public string FileName
        {
            get
            {
                return this.FileNameValue;
            }
            set
            {
                if (value != this.FileNameValue)
                {
                    this.FileNameValue = value;
                    NotifyPropertyChanged("FileName");
                }
            }
        }
        public string Date
        {
            get
            {
                return this.DateValue;
            }
            set
            {
                if (value != this.DateValue)
                {
                    this.DateValue = value;
                    NotifyPropertyChanged("Date");
                }
            }
        }
        public string KeyWords
        {
            get
            {
                return this.KeyWordsValue;
            }
            set
            {
                if (value != this.KeyWordsValue)
                {
                    this.KeyWordsValue = value;
                    NotifyPropertyChanged("KeyWords");
                }
            }
        }


        public bool Include
        {
            get
            {
                return this.IncludeValue;
            }
            set
            {
                if (value != this.IncludeValue)
                {
                    this.IncludeValue = value;
                    NotifyPropertyChanged("Include");
                }
            }
        }
    }

}
